#include <RenderTargetEGL.hpp>

namespace Renderer
{
/*
				return;
			if(rtt == NullPtr) //unset render target
			{
				glViewport( 0, 0, mViewport->GetWidth(), mViewport->GetHeight());
				glBindFramebuffer(GL_FRAMEBUFFER, 0);
				mRenderTarget = NullPtr;
				return;
			}

			//
			IDK::IRenderTarget::RenderTextures rTextures = rtt->GetRenderTextures();
			iAssert(rTextures->Length() != 0, "Need attached render texture");

			ref<IDK::IRenderTexture> tex = rTextures[0]; //note: get only first texture size(other texture have this size)
			//ref<IDK::IRenderTexture> depth = rtt->GetDepthTexture(); 
			//
			HandleGL handleRT = rtt->GetFrameHandle().As<THandleGL>();

			if(handleRT == NullPtr)
			{
				iLog("Frame buffer create: ...");

				GLuint frameID = 0;
				uint depthBuffer = 0;// mRenderHandle->To<GLuint>();
				glGenFramebuffers(1, &frameID);

				iGLDebug("Frame Buffer:", frameID);

				glBindFramebuffer(GL_FRAMEBUFFER, frameID);
				{
					glGenRenderbuffers(1, &depthBuffer);

					//to Log
					iGLDebug("Render Buffer:", depthBuffer);

					glBindRenderbuffer(GL_RENDERBUFFER, depthBuffer);
					glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT16, tex->GetWidth(), tex->GetHeight());
					glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, depthBuffer);

					rtt->SetRenderHandle(new HandleGL::Create(depthBuffer)); //set ID's
				}

				// Iterate render texture and apply
				uint index = 0;

				for(IDK::IRenderTarget::RenderTextures::Iterator
					it = rTextures->Begin(),
					end = rTextures->End(); it!=end; ++it)
				{
					// Need prepare texture
					it->ApplyTexture(this, index);
					HandleGL handleTexture = it->GetHandle().As<THandleGL>();

					// Debug check:)
					iAssert(handleTexture!=NullPtr, "Texture is null, cant attach to rtt");
					uint handleTexID = handleTexture->GetData();
					glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0 + index, GL_TEXTURE_2D, handleTexID, 0);
					SetTexture(NullPtr, index, false);//unset texture
					++index; //next index texture 
				}

				rtt->SetFrameHandle(new HandleGL::Create(frameID)); //set ID's

				const char* FrameBufferErrorMessage[] =
				{
					"Framebuffer Complete",
					"Incomplete attachment",
					"Missing attachment",
					"Duplicate attachment",
					"Incomplete dimensions",
					"Incomplete formats",
					"Incomplete draw buffer",
					"Incomplete read buffer",
					"Framebuffer unsupported"
				};

				GLenum status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
				if(status != GL_FRAMEBUFFER_COMPLETE)
				{
					iLog("Frame buffer error:");
					iLog(FrameBufferErrorMessage[status-GL_FRAMEBUFFER_COMPLETE]);
				}
				else
				{
					iLog("Frame buffer create: OK");
				}
			}
			else
			{
				glBindFramebuffer(GL_FRAMEBUFFER, handleRT->GetData());
			}


			GLenum buffers [] =
			{
				GL_COLOR_ATTACHMENT0,
				GL_COLOR_ATTACHMENT0 + 1,
				GL_COLOR_ATTACHMENT0 + 2,
				GL_COLOR_ATTACHMENT0 + 3,
				GL_COLOR_ATTACHMENT0 + 4,
				GL_COLOR_ATTACHMENT0 + 5,
				GL_COLOR_ATTACHMENT0 + 6,
			};

			//glReadBuffer         (GL_COLOR_ATTACHMENT0_EXT);
			//glDrawBuffer         (GL_COLOR_ATTACHMENT0_EXT);
			glViewport(0, 0, tex->GetWidth(), tex->GetHeight());



			//glDrawBuffers(rtt->GetRenderTextures()->Length(), buffers);

			glClearColor(1.0, 1.0, 1.0, 1.0);
			glClear(GL_COLOR_BUFFER_BIT);
	*/
}